﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media.Animation;

namespace AgLux
{
    /// <summary>
    /// Represents a control that displays a course section component.
    /// </summary>

    public class Component : Control
    {
        public event EventHandler HeightChanged;
        public event EventHandler FullScreenEntered;
        public event EventHandler FullScreenLeft;

        protected FrameworkElement _resource;
        protected Canvas _screen;
        protected TextBlock _caption;

        protected Storyboard _waiting;
        protected bool _isFullScreen;

        /// <summary>
        /// Initializes a new instance of the Component class.
        /// </summary>

        protected Component()
            : base()
        {
            _resource = InitializeFromXaml(new StreamReader(GetType().Assembly.GetManifestResourceStream("eLearning.Component.xaml")).ReadToEnd());
            _screen = (Canvas)_resource.FindName("screen");
            _caption = (TextBlock)_resource.FindName("caption");

            _waiting = null;
            _isFullScreen = false;
            
            Loaded += new EventHandler(Wait);
            BrowserHost.FullScreenChange += new EventHandler(CheckFullScreen);
        }

        /// <summary>
        /// Prepare to display the control.
        /// </summary>

        /// <param name="width">
        /// The width of the canvas on which the control will be displayed.
        /// </param>

        public virtual void Prepare(double width)
        {
            Width = width;
        }

        /// <summary>
        /// Ends the activity of the control.
        /// </summary>

        public virtual void End()
        {
            _waiting = null;
        }

        /// <summary>
        /// Gets or sets the height of the control.
        /// </summary>

        protected virtual new double Height
        {
            get
            {
                return base.Height;
            }

            set
            {
                base.Height = value;
                OnHeightChanged(new EventArgs());
            }
        }

        /// <summary>
        /// Sets the caption of the control.
        /// </summary>

        protected virtual void SetCaption(double width)
        {
            if (_caption.Text != null)
            {
                _caption.Width = width;
                _caption.Height = _caption.ActualHeight;
                _caption.Visibility = Visibility.Visible;
            }
        }

        /// <summary>
        /// Occurs when the control is loaded.
        /// </summary>

        /// <param name="sender">
        /// The object that raised the event.
        /// </param>

        /// <param name="args">
        /// The EventArgs that contains the event data.
        /// </param>

        protected virtual void Wait(object sender, EventArgs args)
        {
            _waiting = (Storyboard)_resource.FindName("waiting");
            _waiting.Completed += new EventHandler(Start);
            _waiting.Begin();
        }

        /// <summary>
        /// Occurs when it is time to start the activity of the control.
        /// </summary>

        /// <param name="sender">
        /// The object that raised the event.
        /// </param>

        /// <param name="args">
        /// The EventArgs that contains the event data.
        /// </param>

        protected virtual void Start(object sender, EventArgs args)
        {
            _waiting = null;
        }

        /// <summary>
        /// Occurs when full screen is changed.
        /// </summary>

        /// <param name="sender">
        /// The object that raised the event.
        /// </param>

        /// <param name="args">
        /// The EventArgs that contains the event data.
        /// </param>

        protected virtual void CheckFullScreen(object sender, EventArgs args)
        {
            if (!BrowserHost.IsFullScreen && _isFullScreen)
            {
                LeaveFullScreen(sender, new MouseEventArgs());
            }
        }

        /// <summary>
        /// Occurs when it is time to enter full screen.
        /// </summary>

        /// <param name="sender">
        /// The object that raised the event.
        /// </param>

        /// <param name="args">
        /// The MouseEventArgs that contains the event data.
        /// </param>

        protected virtual void EnterFullScreen(object sender, MouseEventArgs args)
        {
            BrowserHost.IsFullScreen = _isFullScreen = true;
            OnFullScreenEntered(new EventArgs());
        }

        /// <summary>
        /// Occurs when it is time to leave full screen.
        /// </summary>

        /// <param name="sender">
        /// The object that raised the event.
        /// </param>

        /// <param name="args">
        /// The MouseEventArgs that contains the event data.
        /// </param>

        protected virtual void LeaveFullScreen(object sender, MouseEventArgs args)
        {
            BrowserHost.IsFullScreen = _isFullScreen = false;
            OnFullScreenLeft(new EventArgs());
        }

        /// <summary>
        /// Raises the HeightChanged event.
        /// </summary>

        /// <param name="args">
        /// The EventArgs that contains the event data.
        /// </param>

        protected virtual void OnHeightChanged(EventArgs args)
        {
            if (HeightChanged != null)
            {
                HeightChanged(this, args);
            }
        }

        /// <summary>
        /// Raises the FullScreenEntered event.
        /// </summary>

        /// <param name="args">
        /// The EventArgs that contains the event data.
        /// </param>

        protected virtual void OnFullScreenEntered(EventArgs args)
        {
            if (FullScreenEntered != null)
            {
                FullScreenEntered(this, args);
            }
        }

        /// <summary>
        /// Raises the FullScreenLeft event.
        /// </summary>

        /// <param name="args">
        /// The EventArgs that contains the event data.
        /// </param>

        protected virtual void OnFullScreenLeft(EventArgs args)
        {
            if (FullScreenLeft != null)
            {
                FullScreenLeft(this, args);
            }
        }
    }
}
